﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Server
{
    public partial class ServerForm : Form
    {
        private const string startTheServerAction = "Start the server";
        private const string stopTheServerAction = "Stop the server and quit";
        private const string serverUpStatus = "Server is up";
        private const string serverDownStatus = "Server is down";

        public ServerForm()
        {
            InitializeComponent();
        }

        private void ServerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            ServerConnectionManager.Instance.StopServer();
            System.Environment.Exit(System.Environment.ExitCode);
        }

        private void startStopServerButton_Click(object sender, EventArgs e)
        {
            if (startStopServerButton.Text == startTheServerAction && startStopServerLabel.Text == serverDownStatus)
            {
                try
                {
                    ServerConnectionManager.Instance.StartServer();
                }
                catch (ConnectionException)
                {
                    MessageBox.Show("No clients connected", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                startStopServerButton.Text = stopTheServerAction;
                startStopServerLabel.Text = serverUpStatus;
            }
            else if (startStopServerButton.Text == stopTheServerAction && startStopServerLabel.Text == serverUpStatus)
            {
                ServerConnectionManager.Instance.StopServer();
                System.Environment.Exit(System.Environment.ExitCode);
            }
        }
    }
}
